package com.hlhj.funlike_businessqzxd.activity

import android.graphics.Color
import android.util.Log
import android.widget.Toast
import com.example.mymvp.mvp.BaseModule
import com.example.mymvp.mvp.BasePresenter
import com.example.mymvp.mvp.BaseView
import com.example.mymvp.okrx.BaseBean
import com.google.gson.Gson
import com.hlhj.funlike_businessqzxd.R
import com.hlhj.funlike_businessqzxd.constant.Constants
import com.hlhj.funlike_businessqzxd.data.SPUser
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import kotlinx.android.synthetic.main.aty_color.*
import wang.relish.colorpicker.ColorPickerDialog
import java.lang.Exception

class ColorPickAty :BaseActivity<BaseView,BasePresenter<BaseModule,BaseView>>(), BaseView {
    override fun initView() {
        try {
            colorView.setBackgroundColor(Color.parseColor(intent.getStringExtra("color")))

        }catch (e:Exception){

        }
    }
    var sColor=""
    override fun logicStart() {
    }

    override fun setListener() {
        colorView.setOnClickListener {
            ColorPickerDialog.Builder(this, colorView.drawingCacheBackgroundColor)   //mColor:初始颜色
                    .setHexValueEnabled(false)               //是否显示颜色值
                    .setOnColorChangedListener {
                        i->
                colorView.setBackgroundColor(i);
                        sColor= Integer.toHexString(i);
                        sColor=  sColor.substring(2)
                        Log.e("fhp","看一下直接获取的颜色"+sColor)
                 /*
                String hexString = Integer.toHexString(i);
                String s = hexString.substring(2);
                OkGo.<String>post(Constants.changeColor)
                        .params("color",s)
                        .execute(new StringCallback() {
                            @Override
                            public void onSuccess(Response<String> response) {
                                BaseBean bean = new Gson().fromJson(response.body(), BaseBean.class);
                                if (bean.getCode()==1
                                ){
                                    Toast.makeText(SettingsAty.this,"修改成功",Toast.LENGTH_SHORT).show();
                                }else {
                                    Toast.makeText(SettingsAty.this,bean.getMsg(),Toast.LENGTH_SHORT).show();
                                }
                            }
                        });*/
                    } //设置监听颜色改变的监听器
                    .build().show()
        }
        tvStats.setOnClickListener {

            OkGo.post<String>(Constants.changeColor)
                    .headers("token",SPUser.getUser(this).token)
                    .params("color","#${sColor}")
                    .execute(object :StringCallback(){
                        override fun onSuccess(response: Response<String>?) {
                            val bean = Gson().fromJson(response?.body(), BaseBean::class.java)
                            if (bean!!.code==1
                            ){
                                Toast.makeText(this@ColorPickAty,"修改成功",Toast.LENGTH_SHORT).show();
                                finish()
                            }else {
                                Toast.makeText(this@ColorPickAty,bean.msg,Toast.LENGTH_SHORT).show();
                            }
                        }
                    })
        }
        img_back.setOnClickListener {
            finish()
        }
    }

    override fun getContentId(): Int {
        return R.layout.aty_color

    }

    override fun createPresenter(): BasePresenter<BaseModule, BaseView> {
        return object :BasePresenter<BaseModule,BaseView>(){
            override fun createModule() {


            }
        }
    }

    override fun initData() {
    }

    override fun bindView(): BaseView {
        return this
    }
}